home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / ADDPATH.C next >
Text File  |  1990-04-13  |  1KB  |  54 lines

  1. /*
  2.  * addpath.c
  3.  *
  4.  * Returned status:
  5.  *    0    OK (including case where nothing needs to be done)
  6.  *    1    bad arguments
  7.  *
  8.  */
  9. #include <io.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. void exit(int status) { _exit(status); }
  14.  
  15. #define writes(s,i)  write(i,s,strlen(s));
  16.  
  17. main(int argc, char *argv[])
  18. {
  19.     int len;
  20.     char *path, *newdir, *p;
  21.  
  22.     if( argc != 2)    {
  23.         writes("Usage: addpath pathname\n",2);
  24.         exit(1);
  25.     } else {
  26.         newdir = strupr(argv[1]);
  27.         len = strlen(newdir);
  28.         /* strip off a trailing ";" and upper-case */
  29.         if( *(newdir+len-1) == ';') *(newdir+len-1) = '\0';
  30.         path = getenv("PATH");
  31.  
  32.         writes(path,1);
  33.         if ((p = strstr(path,newdir)) == NULL) {
  34.             /* not in path already - add it */
  35.             writes(";",1);
  36.             writes(newdir,1);
  37.         } else {
  38.             /* we found it, but it may be a partial match */
  39.             if (    ((p == path) || (p != path && *(p-1) == ';'))
  40.                  && (*(p+len) == ';' || *(p+len) == '\0') ) {
  41.                 /* already in path */
  42.             } else {
  43.                 /* it was a bogus match - add the new one */
  44.                 writes(";",1);
  45.                 writes(newdir,1);
  46.             }
  47.  
  48.         }
  49.     }
  50.     return(0);
  51. }
  52.  
  53.  
  54.